import React, { useState } from "react";
import ClassCom from "./components/classCom";
import FuncCom from "./components/funcCom";
import My from "./components/my";
import Login from "./components/login";
import {
  BrowserRouter,
  Routes,
  Route,
  HashRouter,
  Navigate,
  useRoutes,

} from "react-router-dom";

const PrivateRouter = (props: any) => {
  const token = localStorage.getItem('token');

  if(token) {
    return props.children
  }

  return <Navigate to="/login/1/张三"></Navigate>

};

const routes = [
  {
    path: "/",
    element: <FuncCom></FuncCom>,
    children: [
      {
        path: "/my",
        element: <My></My>,
      },
    ],
  },
  {
    path: "/class",
    element: (
      <PrivateRouter>
        <ClassCom message="哈哈"></ClassCom>
      </PrivateRouter>
    ),
  },
  {
    path: "/login/:id/:name",
    element: <Login></Login>,
  },
];

const App = () => {
  const [visible, setVisble] = useState(true);
  const Router = () => useRoutes(routes);

  return (
    <div>
      <BrowserRouter>
        <Router></Router>
      </BrowserRouter>
    </div>
  );
};

export default App;
